package com.guxingke.gsb.ui.cli;

import com.guxingke.gsb.Gsb;
import com.guxingke.gsb.config.Constant;
import com.guxingke.gsb.util.PathUtil;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.ParserProperties;
import org.kohsuke.args4j.spi.BooleanOptionHandler;
import org.kohsuke.args4j.spi.IntOptionHandler;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * 在当前目录执行初始化站点接口
 *
 * @author guxingke admin@guxingke.com
 */
public class PreviewCommand implements GsbCommand {
    @Option(name = "--port", aliases = {"-p"}, metaVar = "<port_number>", handler = IntOptionHandler.class, usage = "Port on which to launch the preview. Default to your configuredData. port.")
    private Integer port = Constant.DEFAULT_PREVIEW_PORT;

    @Option(name = "--help", aliases = {"-h"}, handler = BooleanOptionHandler.class, usage = "find help about this command")
    private boolean help = false;

    @Option(name = "--action", aliases = {
            "-a"}, metaVar = "<action_name>", usage = "preview action")
    private String action = "start";

    /**
     * Executes the command
     */
    @Override
    public void execute() {
        Gsb gsb = Gsb.getInstance();
        if (help) {
            System.out.println("Preview the site on the given port: default: 9090");
            System.out.println("usage: gsb preview [option]");
            System.out.println("Options: " + PathUtil.LINE_SEPARATOR);
            CmdLineParser parser = new CmdLineParser(this, ParserProperties.defaults().withUsageWidth(120));
            parser.printUsage(System.out);
        }
        else {
            if ("start".equals(action)){
                gsb.preview(port);
            }else {
                gsb.previewStop(port);
            }
        }
    }
}
